package com.tgy.leetcode;

public class _832_翻转图像 {

    public int[][] flipAndInvertImage(int[][] A) {
        int row = A.length;
        int col = A[0].length;

        for (int i = 0; i < row; i++) {

            int left = 0, right = col - 1 - left;
            while (left <= right) {

                // 异或 无符号相加
                int tmp = A[i][left] ^ 1;
                A[i][left] = A[i][right] ^ 1;
                A[i][right] = tmp;

                left++;
                right--;
            }
        }

        return A;
    }

    public int[][] flipAndInvertImage01(int[][] A) {

        int row = A.length;
        int col = A[0].length;

        for (int i = 0; i < row; i++) {

//            int half = (col >> 1);
//
//            for (int j = 0; j < half; j++) {
//
//                int tmp = A[i][j];
//                A[i][j] = A[i][col - 1 - j];
//                A[i][col - j] = tmp;
//            }

            int left = 0, right = col - 1 - left;

            while (left < right) {

                // 异或 无符号相加
                int tmp = A[i][left] ^ 1;
                A[i][left] = A[i][right] ^ 1;
                A[i][right] = tmp;

                left++;
                right--;
            }

            for (int j = 0; j < col; j++) {

                if (A[i][j] == 0) {

                    A[i][j] = 1;
                } else {

                    A[i][j] = 0;
                }
            }
        }

        return A;
    }

    public static void main(String[] args) {

    }
}
